package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.SellLevelDAO;
import com.junzhiit.nengji.model.SellLevel;

@Repository
public class SellLevelDAOImpl extends AbstractBaseDAO<SellLevel> implements SellLevelDAO {

	@Override
	public List<SellLevel> findAll(SellLevel sellLevel, int pageNumber, int maxPageRow) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from sell_level");
		sql.append(" where is_delete = ?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(sellLevel.getName())){
			sql.append(" AND name like ?");
			param.setString("%" + sellLevel.getName() + "%");
		}
		
		sql.append(" order by level asc");
		
		return super.queryForPagingList(sql.toString(), SellLevel.class, param, pageNumber, maxPageRow);
	}

	@Override
	public int getTotalCount(SellLevel sellLevel) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from sell_level");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		sql.append(" order by level asc");
		
		return super.queryForInt(sql.toString(),param);
	}

	@Override
	public int getSellLevelMax() {
		StringBuffer sql=new StringBuffer();
		sql.append(" select ifnull(max(level),0) from sell_level");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.queryForInt(sql.toString(),param);
	}

	@Override
	public List<SellLevel> getList() {
		String sql = "select * from sell_level where is_delete= ? order by level asc";
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		return super.queryForList(sql, SellLevel.class,params);
	}

	public SellLevel findByLevel(int level) {
		String sql = "select * from sell_level where level = ? and  is_delete = ?";
		StatementParameter params = new StatementParameter();
		params.setInt(level);
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql, SellLevel.class, params);
	}

	
}
